Completed
Push — master ( 72dc97...64dc81 )
by greg
01:57
created

post.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
nop 1
1
import path from 'path'
2
3
import {
4
  cmsData,
5
  cmsOperations,
6
  coreUtils,
7
  config,
8
  abeExtend,
9
  Manager
10
} from '../../'
11
12 View Code Duplication
export function publish(filePath, tplPath, json) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
13
  var p = new Promise((resolve, reject) => {
14
    abeExtend.hooks.instance.trigger('beforePublish', json, filePath, tplPath)
15
    var p1 = new Promise((resolve) => {
16
      cmsOperations.save.save(
17
        path.join(config.root, config.draft.url, filePath.replace(config.root)),
18
        tplPath,
19
        json,
20
        '',
21
        'draft',
22
        null,
23
        'publish')
24
        .then(() => {
25
          resolve()
26
        }).catch(function(e) {
27
          console.error(e)
28
        })
29
    })
30
31
    p1.then((resSave) => {
32
      cmsOperations.save.save(
33
        path.join(config.root, config.draft.url, filePath.replace(config.root)),
34
        tplPath,
35
        json,
36
        '',
37
        'publish',
38
        resSave,
39
        'publish')
40
        .then((resSave) => {
41
          var result
42
          if(typeof resSave.error !== 'undefined' && resSave.error !== null  ){
43
            result = {
44
              success: 0,
45
              error: resSave.error
46
            }
47
          } else if(typeof resSave.reject !== 'undefined' && resSave.reject !== null){
48
            result = resSave
49
          } else if(typeof resSave.json !== 'undefined' && resSave.json !== null){
50
            result = {
51
              success: 1,
52
              json: resSave.json
53
            }
54
          }
55
          abeExtend.hooks.instance.trigger('afterPublish', result)
0 ignored issues
show
Bug introduced by
The variable result does not seem to be initialized in case typeof resSave.json !==...& resSave.json !== null on line 49 is false. Are you sure the function trigger handles undefined variables?
Loading history...
56
          Manager.instance.updateList()
57
          resolve(result)
58
        }).catch(function(e) {
59
          console.error('post.js', e)
60
          var result = {
61
            success: 0,
62
            error: 'publish error'
63
          }
64
          abeExtend.hooks.instance.trigger('afterPublish', result)
65
          resolve(result)
66
        })
67
    }).catch(function(e) {
68
      console.error('post.js', e)
69
      var result = {
70
        success: 0,
71
        error: 'publish error'
72
      }
73
      abeExtend.hooks.instance.trigger('afterPublish', result)
74
      resolve(result)
75
    })
76
  })
77
78
  return p
79
}
80
81
export function unpublish(filePath) {
82
  abeExtend.hooks.instance.trigger('beforeUnpublish', filePath)
83
  filePath = coreUtils.slug.clean(filePath)
84
  var tplUrl = cmsData.file.fromUrl(path.join(config.publish.url, filePath))
85
  if(coreUtils.file.exist(tplUrl.json.path)) {
86
    var json = JSON.parse(JSON.stringify(cmsData.file.get(tplUrl.json.path)))
87
    if(json.abe_meta.publish != null) {
88
      delete json.abe_meta.publish
89
    }
90
91
    cmsOperations.save.save(
92
      path.join(config.root, config.draft.url, json.abe_meta.link.replace(config.root)),
93
      json.abe_meta.template,
94
      json,
95
      '',
96
      'reject',
97
      null,
98
      'reject'
99
    )
100
    .then(() => {
101
      cmsOperations.remove.removeFile(tplUrl.publish.path, tplUrl.publish.json)
102
      abeExtend.hooks.instance.trigger('afterUnpublish', tplUrl.publish.path, tplUrl.publish.json)
103
      Manager.instance.updateList()
104
    })
105
  }
106
}
107
108 View Code Duplication
export function reject(filePath, tplPath, json) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
109
  abeExtend.hooks.instance.trigger('beforeReject', filePath)
110
  
111
  var p = new Promise((resolve) => {
112
    cmsOperations.save.save(
113
      path.join(config.root, config.draft.url, filePath.replace(config.root)),
114
      tplPath,
115
      json,
116
      '',
117
      'draft',
118
      null,
119
      'reject')
120
      .then(() => {
121
        resolve()
122
      }).catch(function(e) {
123
        console.error(e)
124
      })
125
  })
126
127
  p.then((resSave) => {
128
    cmsOperations.save.save(
129
      path.join(config.root, config.draft.url, filePath.replace(config.root)),
130
      tplPath,
131
      json,
132
      '',
133
      'reject',
134
      resSave,
135
      'reject')
136
      .then((resSave) => {
137
        var result
138
        if(typeof resSave.error !== 'undefined' && resSave.error !== null  ){
139
          result = {
140
            success: 0,
141
            error: resSave.error
142
          }
143
        } else if(typeof resSave.reject !== 'undefined' && resSave.reject !== null){
144
          result = resSave
145
        } else if(typeof resSave.json !== 'undefined' && resSave.json !== null){
146
          result = {
147
            success: 1,
148
            json: resSave.json
149
          }
150
        }
151
        abeExtend.hooks.instance.trigger('afterReject', result)
0 ignored issues
show
Bug introduced by
The variable result does not seem to be initialized in case typeof resSave.json !==...& resSave.json !== null on line 145 is false. Are you sure the function trigger handles undefined variables?
Loading history...
152
        Manager.instance.updateList()
153
        resolve(result)
154
      })
155
  }).catch(function(e) {
156
    console.error(e)
157
    var result = {
158
      success: 0,
159
      error: 'reject error'
160
    }
161
    abeExtend.hooks.instance.trigger('afterReject', result)
162
    resolve(result)
163
  }) 
164
}